/*
   Chest Class
   
   This is the scripted version of the chests that are laid
   using the level editor. Feel free to edit this class to
   meet the needs of your own server.
   
   At default if you don't specify an item in the following
   list it will automatically try to add the weapon that
   goes by that name.
   
   Chest Items:
   
   - "greenrupee"
   - "bluerupee"
   - "redrupee"
   - "goldrupee"
   - "bombs"
   - "darts"
   - "glove1"
   - "glove2"
   - "heart"
   - "fullheart"
   - "sword"
   - "battleaxe"
   - "lizardsword"
   - "shield"
   - "mirrorshield"
   - "lizardshield"
   
   Level Usage:
   
function onCreated() {
  // Chest Variables
  this.treasure_id      = "uniquename";
  this.treasure_item    = "greenrupee";
  // Join class script
  this.join("object_chest");
}

*/

function onCreated() {
  // Give chest shape
  this.setshape(1, 32, 32);
  // Initialize chest variables
  this.attr[1] = this.treasure_id;
  this.attr[2] = getTreasureGraphic(this.treasure_item);
}

function getTreasureGraphic(treasureItem) {
  // Determine Graphic Name by treasureItem
  switch (treasureItem) {
    case "greenrupee":
    case "bluerupee":
    case "redrupee":
    case "goldrupee":
      temp.rupeecolor = treasureItem.substring(0, treasureItem.pos("rupee"));
      temp.gfx = "chest_" @ temp.rupeecolor @ "gralat.png";
      break;
    case "bombs":
    case "darts":
    case "glove1":
    case "glove2":
    case "heart":
    case "fullheart":
    case "sword":
    case "battleaxe":
    case "lizardsword":
    case "shield":
    case "mirrorshield":
    case "lizardshield":
      temp.gfx = "chest_" @ treasureItem @ ".png";
      break;
    default:
      // Resort to weapon script's graphic.
      temp.gfx = findweapon(treasureItem).image;
      break; 
  }
  // Return graphic
  return temp.gfx;
}

function onActionOpenChest(chestID) {
  // Make sure ID matches trigger
  if (chestID == this.id) {
    // Check if opened
    if (!isOpened()) {
      // Set open flag
      setOpened();
      // Award treasure
      awardTreasure(this.treasure_item);
    }
  }
}

function awardTreasure(treasureItem) {
  // Award treasure based on treasureItem
  switch (treasureItem) {
    // Items
    case "greenrupee": player.rupees += 1;   break;
    case "bluerupee":  player.rupees += 5;   break;
    case "redrupee":   player.rupees += 30;  break;
    case "goldrupee":  player.rupees += 100; break;
    case "heart":      player.hearts += 1;   break;
    case "bombs":      player.bombs  += 5;   break;
    case "darts":      player.darts  += 5;   break;
    // Full Heart
    case "fullheart":  player.fullhearts++;  break;
    // Shields
    case "shield":        player.setshield("shield1.png", 1); break;
    case "mirrorshield":  player.setshield("shield2.png", 2); break;
    case "lizardshield":  player.setshield("shield3.png", 3); break;
    // Swords
    case "sword":        player.setsword("sword1.png", 1); break;
    case "battleaxe":    player.setsword("sword2.png", 2); break;
    case "lizardsword":  player.setsword("sword3.png", 3); break;
    // Gloves
    case "glove1":
    case "glove2":
      // Define Gloves Power
      temp.newpower = treasureItem.charat("glove".length());
      // Check if change is required
      if (temp.newpower > player.glovepower) {
        // Update glovepower
        player.glovepower = temp.newpower;
      }
      break;
    default:
      // Add weapon to player
      player.addweapon(treasureItem);
      break;
  }
}

function setOpened() {
  // Set chest flag to true
  clientr.("chest_" @ this.treasure_id) = true;
}

function isOpened() {
  // Return chest flag
  return clientr.("chest_" @ this.treasure_id);
}

//#CLIENTSIDE

function onCreated() {
  // Update chest image
  updateChest();
}

function onPlayerEnters() {
  // Update chest image
  updateChest();
}

function updateChest() {
  // Update chest imagery
  setOpenState(isOpened());
}

function setOpenState(isOpened) {
  // Define chest images
  temp.opened = "chest.png";
  temp.closed = "chestopen.png";
  // Change image
  this.image = isOpened ? temp.closed : temp.opened;
  // Give shape
  this.setshape(1, 32, 32);
}

function onPlayerTouchsMe() {
  // Check if chest is open
  if (!isOpened()) {
    // Begin opening chest
    openChest();
  }
}

function openChest() {
  // Check if opening
  if (!this.opening) {
    // Set opening flag
    this.opening = true;
    // Set open state
    setOpenState(true);
    // SFX
    playSFX();
    // Display treasure image
    displayTreasure();
    // Trigger openChest on serverside.
    triggeraction(this.x + 1, this.y + 1, "openChest", this.id);
  }
}

function playSFX() {
  // Play sound effect
  play("chest.wav");
}

function displayTreasure() {
  // Display treasure graphic
  temp.img = showimg(200, this.attr[2], this.x, this.y - 1);
  temp.img.mode = 1;
  // Schedule hide graphic event
  this.scheduleevent(3, "HideTreasure", "");
}

function onHideTreasure() {
  // Fade-out treasure graphic
  with (findimg(200)) {
    // Decrement Hide
    alpha -= 0.05;
    // Check if completely hidden
    if (alpha <= 0) temp.hide = true;
  }
  if (temp.hide) {
    // Hide image completely
    hideimg(200);
  } else {
    // Continue Fading
    this.scheduleevent(0.05, "HideTreasure", "");
  }
}

function isOpened() {
  // Return chest flag
  return clientr.("chest_" @ this.attr[1]);
}
